package com.linkhub.oms.data.sync.service.impl;

import com.dtflys.forest.http.ForestResponse;
import com.linkhub.oms.data.sync.api.track.client.TrackClient;
import com.linkhub.oms.data.sync.api.track.domain.request.TrackBaseRequest;
import com.linkhub.oms.data.sync.api.track.domain.response.TrackAcceptedBase;
import com.linkhub.oms.data.sync.api.track.domain.response.TrackBaseResponse;
import com.linkhub.oms.data.sync.api.track.domain.response.TrackRegisterAcceptedData;
import com.linkhub.oms.data.sync.service.TrackApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Redball
 * @version 1.0.0
 * @Description TrackApiServiceImpl
 * @createTime 2024/10/27 上午1:42
 */
@Slf4j
@Service
public class TrackApiServiceImpl implements TrackApiService {

    @Resource
    private TrackClient trackClient;

    private static final int MAX_RETRIES = 3;

    @Override
    public TrackBaseResponse<TrackRegisterAcceptedData> registerTrack(List<TrackBaseRequest> request) {
        for (int attempt = 1;; attempt++) {
            try {
                ForestResponse<TrackBaseResponse<TrackRegisterAcceptedData>> forestResponse = trackClient.register(request);
                return forestResponse.getResult();
            } catch (Exception e) {
                log.error("Failed to register track, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
    }

    @Override
    public TrackBaseResponse<TrackAcceptedBase> stopTrack(List<TrackBaseRequest> request) {
        for (int attempt = 1;; attempt++) {
            try {
                ForestResponse<TrackBaseResponse<TrackAcceptedBase>> forestResponse = trackClient.stopTrack(request);
                return forestResponse.getResult();
            } catch (Exception e) {
                log.error("Failed to stop track, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
    }

    @Override
    public TrackBaseResponse<TrackAcceptedBase> reTrack(List<TrackBaseRequest> request) {
        for (int attempt = 1;; attempt++) {
            try {
                ForestResponse<TrackBaseResponse<TrackAcceptedBase>> forestResponse = trackClient.reTrack(request);
                return forestResponse.getResult();
            } catch (Exception e) {
                log.error("Failed to reTrack, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
    }

    @Override
    public TrackBaseResponse<TrackAcceptedBase> deleteTrack(List<TrackBaseRequest> request) {
        for (int attempt = 1;; attempt++) {
            try {
                ForestResponse<TrackBaseResponse<TrackAcceptedBase>> forestResponse = trackClient.deleteTrack(request);
                return forestResponse.getResult();
            } catch (Exception e) {
                log.error("Failed to delete track, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
    }

    @Override
    public TrackBaseResponse<TrackAcceptedBase> pushTrack(List<TrackBaseRequest> request) {
        for (int attempt = 1;; attempt++) {
            try {
                ForestResponse<TrackBaseResponse<TrackAcceptedBase>> forestResponse = trackClient.push(request);
                return forestResponse.getResult();
            } catch (Exception e) {
                log.error("Failed to push track, attempt {} of {}", attempt, MAX_RETRIES, e);
                if (attempt == MAX_RETRIES) {
                    throw e;
                }
            }
        }
    }
}
